package dynamicprogramming;

/**
 * @Author: LDeng
 * @Date: 2021-04-27 13:52
 */
public class LCSubString {

    public static void main(String[] args) {
        System.out.println(lcs("ABCD", "BABC"));
        System.out.println(lcs1("ABCD", "BABC"));
    }
    //一维数组实现
    static int lcs1(String str1,String str2){
        if (str1 == null || str2 == null) return 0;
        char[] chars1 = str1.toCharArray();
        if (chars1.length == 0) return 0;
        char[] chars2 = str2.toCharArray();
        if (chars2.length == 0) return 0;
        char[] rowsChars=chars1;
        char[] colsChars=chars2;
        if(chars1.length<chars2.length){
            colsChars=chars1;
            rowsChars=chars2;
        }
        int[] dp= new int[colsChars.length+1];
        int max=0;
        for (int row = 1; row <=colsChars.length; row++) {
            int current=0;
            for (int col = 1; col <=rowsChars.length; col++) {
                int leftTop=current;
                current=dp[col];
                if(chars1[row-1]!=chars2[col-1]){
                    dp[col]=0;
                }else{
                    dp[col]=leftTop+1;
                    max = Math.max(dp[col], max);
                }
            }
        }
        return max;
    }

    //lcs 二维数组实现
    static int lcs(String str1, String str2) {
        if (str1 == null || str2 == null) return 0;
        char[] chars1 = str1.toCharArray();
        if (chars1.length == 0) return 0;
        char[] chars2 = str2.toCharArray();
        if (chars2.length == 0) return 0;
        int[][] dp = new int[chars1.length + 1][chars2.length + 1];
        //初始dp[x][x] 为0；
        int max = 0;
        for (int i = 1; i <= chars1.length; i++) {
            for (int j = 1; j <= chars2.length; j++) {
                if (chars1[i - 1] != chars2[j - 1]) continue;//不相等 默认为0
                dp[i][j] = dp[i - 1][j - 1] + 1;//相等， 前面的数+1
                max = Math.max(dp[i][j], max);
            }
        }
        return max;
    }

}
